﻿#include "mylabel.h"
#include <QPaintEvent>
#include <QMouseEvent>


/*全局变量*/
bool myLabel::fullScreen=false;//全屏标识
bool myLabel::showBoMen=false;
myLabel::myLabel(QWidget *parent) :
    QLabel(parent)
{
    imageWidth=0;//视频宽度
    imageHeight=0;//视频高度
    imageFrequency=0;//视频帧率
    offset_x=0;        //脱靶量 X
    offset_y=0;        //脱靶量 Y
    offset_A=0;        //脱靶量 A
    offset_E=0;        //脱靶量 E
    nPosX=0;         //目标x坐标
    nPosY=0;          //目标y坐标
    AVelocity=0;       //速度A
    EVelocity=0;       //速度E
//    double  Velocity;        //速度
//    double  size;  //面积
    AForecastValue=0;  //预测A值
    EForecastValue=0;  //预测E值
    menu=NULL;
    fullscreen=NULL;
    exit=NULL;
    boMen=NULL;

}
void myLabel::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);//调用父类paintEvent，避免覆盖自定义paintEvent
    QPainter painter(this);//在当前类实例化QPainter
//    painter.setPen(QPen(Qt::red,1.5*this->width()/672,Qt::DotLine));//设置画笔
//QRectF rect(300,300,100,100);
//painter.drawRect(rect);

   // QRectF rectangle(590*this->width()/672,21*this->height()/378,60*this->width()/672,27*this->height()/378);
    //QRectF rectangle(qrand()%1000,qrand()%1000,100,100);
    ////painter.drawRect(rectangle);//绘制波门
    //qDebug()<<qrand()%100<<lyr.lHeight;
    //painter.setAutoFillBackground(true);

//    QHBoxLayout *myLayout=new QHBoxLayout(this);
//    QLabel *ly=new QLabel(this);
//    ly->setText("womendushizhongguoren");
//    //painter.drawText(20,20,"ly");
//    myLayout->addWidget(ly);




   // painter.setRenderHint(QPainter::Antialiasing, true); //消除锯齿效果

       //构造painterpath
       QPainterPath path;
       path.moveTo(300*this->width()/672, 300*this->height()/378);
       path.lineTo(300*this->width()/672, 0);
       path.lineTo(300/2*this->width()/672, 300*this->height()/672);
       path.lineTo(300*this->width()/672, 300*this->height()/378);

       //path->setFillRule(Qt::WindingFill);
       //设置无画笔，避免边框出现一条黑线
      // painter.setPen(Qt::NoPen);
       //设置画刷
       //painter.setBrush(Qt::SolidPattern);
       //绘制背景
       //painter.drawPath(path);

       QFont font=painter.font();
       font.setPixelSize(15);
       painter.setFont(font);
       painter.setPen(QPen(Qt::white));
       QPointF informationPoint(500,20);   //视频信息
       QPointF videoInfoPoint(10,60);      //
       QPointF tuoBaLiangPoint(10,30);     //脱靶量
       QPointF xiangShuOffsetPoint(80,20); //偏移像素
       QPointF jiaoDuOffsetPoint(80,40);   //偏移角度
       QPointF muBiaoPoint(500,40);        //目标位置

       QPointF suduPoint(80,60);           //速度AE值
       QPointF  yucezhiPoint(500,60);       //预测AE值

       const QRect rectangle=QRect(100,100,1000,500);
       QRect boundingRect=QRect(100,100,1000,500);
       if(this->pixmap()!=NULL)
       {
           painter.drawText(tuoBaLiangPoint,tr("脱靶量"));
           painter.drawText(xiangShuOffsetPoint,tr("偏移像素：X：%1").arg(offset_x)+"     "+tr("Y：%1").arg(offset_y));
           painter.drawText(jiaoDuOffsetPoint,tr("偏移角度：A：%1").arg(offset_A)+"     "+tr("E：%1").arg(offset_E));
           painter.drawText(muBiaoPoint,tr("目标位置：%1     %2").arg(nPosX).arg(nPosY));
           painter.drawText(informationPoint,tr("视频信息：")+tr("%1").arg(imageWidth)
                            +tr("*")+tr("%1").arg(imageHeight)+tr("    ")+tr("%1").arg(imageFrequency));

           painter.drawText(videoInfoPoint,tr("%1").arg(videoInfo));

           painter.drawText(suduPoint,tr("速度：A: %1").arg(AVelocity)+"     "+tr("E: %1").arg(EVelocity));
           painter.drawText(yucezhiPoint,tr("预测值：A: %1").arg(AForecastValue)+"     "+tr("E: %1").arg(EForecastValue));

       }
       else
       {
           painter.drawText(tuoBaLiangPoint,tr("<<<左侧单击选择"));
       }
       QPen pen = painter.pen();
       pen.setStyle(Qt::DotLine);
       painter.setPen(pen);
       //painter.drawRect(boundingRect);

       pen.setStyle(Qt::DashLine);
       painter.setPen(pen);
       //painter.drawRect(rectangle);



    //painter.drawText(200*this->width()/672,200*this->height()/378,tr("LYR:%1\n         ly:%2").arg("good").arg("women"));//绘制元数据信息
    //update();
        event->accept();
}

// void myLabel::mousePressEvent(QMouseEvent *ev)
// {
////     switch (control) {
////     case value:

////         break;
////     default:
////         break;
////     }
// }

 void myLabel::mouseDoubleClickEvent(QMouseEvent *ev)//双击鼠标全屏
 {
     if(!fullScreen)//全屏标识
     {
         fullScreen=!fullScreen;//状态反转
         this->setWindowFlags(Qt::Window);//设置窗口标识
         this->showFullScreen();//全屏显示
     }
     else
     {


         this->setWindowFlags(Qt::Widget);//还原窗口标示
         this->showNormal();//更改显示方式为正常显示
//         this->setGeometry(300,90,(1591*1.0/1920)*screenWidth,(911*1.0/1080)*screenHeight);//重新定位控件
//         this->setGeometry(300,90,1000,678);//重新定位控件
         fullScreen=!fullScreen;//状态反转
     }
     ev->accept();
 }
 void myLabel::mouseDoubleClickEvent()//双击鼠标全屏
 {
     if(!fullScreen)//全屏标识
     {
         fullScreen=!fullScreen;//状态反转
         this->setWindowFlags(Qt::Window);//设置窗口标识
         this->showFullScreen();//全屏显示
     }
     else
     {


         this->setWindowFlags(Qt::Widget);//还原窗口标示
         this->showNormal();//更改显示方式为正常显示
//         this->setGeometry(300,90,(1591*1.0/1920)*screenWidth,(911*1.0/1080)*screenHeight);//重新定位控件
//          this->setGeometry(300,90,1000,678);//重新定位控件
         fullScreen=!fullScreen;//状态反转
     }
 }
void myLabel::contextMenuEvent(QContextMenuEvent *ev)//鼠标右键菜单
{
    if(this->pixmap()!=NULL)
        this->setStyleSheet("white");
    menu=new QMenu(this);
    fullscreen=new QAction(tr("全屏"),this);
    fullscreen->setCheckable(true);
    menu->addAction(fullscreen);
    connect(fullscreen,SIGNAL(triggered()),this,SLOT(mouseDoubleClickEvent()));
    if(fullScreen)                             //如果已经是全屏状态则显示退出全屏
    {
        fullscreen->setText("退出全屏");
    }

    boMen=new QAction(tr("显示波门"),this);
    menu->addAction(boMen);
    connect(boMen,SIGNAL(triggered()),this,SLOT(selectShowBoMen()));
    if(showBoMen)
    {
        boMen->setText("隐藏波门");
    }

    exit=new QAction(tr("退出系统"),this);//退出系统右键菜单
    menu->addAction(exit);
//    connect(exit,SIGNAL(triggered(bool)),this->parent()->parent(),SLOT(close()));//退出应用，注意链接槽的对象
    connect(exit,SIGNAL(triggered(bool)),this->parent(),SLOT(on_pBtnClose_clicked()));//退出应用，注意链接槽的对象
    menu->move(cursor().pos());//移动到鼠标点击位置
    menu->show();//显示出右键菜单


    ev->accept();
}
void myLabel::selectShowBoMen()//波门显示选择
{
    if(!showBoMen)
    {
        myLabel::showBoMen=!showBoMen;
    }
    else
    {
        myLabel::showBoMen=!showBoMen;
    }
}
